/**
 *  @author Ion Morozan
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.dddforandroid.design;

import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;

import com.dddforandroid.database.R;
import com.dddforandroid.database.local.UpdateNote;
import com.dddforandroid.database.server.SyncServerDatabase;

public class ModifyTaskActivity extends Activity{
	
	private SyncServerDatabase nosql;
	
	public static ProgressDialog progress;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.edit_task);
		
		this.nosql = TasksArrayAdapter.nosql;
		String name = (String) getIntent().getExtras().get("name");
		String note = (String) getIntent().getExtras().get("note");
		
		Long Ldate = (Long) getIntent().getExtras().get("date");
		Date date = new Date(Ldate);
		
		final String id = getIntent().getExtras().getString("id");
		
		/* The EditText used*/
        final EditText nameET, noteET;
        final DatePicker datePk;
        
		nameET = (EditText) findViewById(R.id.name);
        noteET = (EditText) findViewById(R.id.note);
        datePk = (DatePicker) findViewById(R.id.pickDate);
        
        nameET.setText(name);
        noteET.setText(note);

        Calendar cal = Calendar.getInstance(Locale.ENGLISH);
        cal.setTime(date);
        
        datePk.updateDate(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), 
        		cal.get(Calendar.DATE));
        
        Button modify = (Button)findViewById(R.id.updateButton);
		/* Listener for the Modify Button */
		modify.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				progress = ProgressDialog.show(ModifyTaskActivity.this, "",
						"Updating...");
				new UpdateNote(nameET, noteET, datePk, id, getApplicationContext(),
						nosql, CurrentTasksActivity.authClient).execute();
			}
		});
	}
}
